home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / server / scripts / item.cs < prev    next >
Encoding:
Text File  |  2005-11-23  |  4.3 KB  |  131 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // These scripts make use of dynamic attribute values on Item datablocks,
  7. // these are as follows:
  8. //
  9. //    maxInventory      Max inventory per object (100 bullets per box, etc.)
  10. //    pickupName        Name to display when client pickups item
  11. //
  12. // Item objects can have:
  13. //
  14. //    count             The # of inventory items in the object.  This
  15. //                      defaults to maxInventory if not set.
  16.  
  17. // Respawntime is the amount of time it takes for a static "auto-respawn"
  18. // object, such as an ammo box or weapon, to re-appear after it's been
  19. // picked up.  Any item marked as "static" is automaticlly respawned.
  20. $Item::RespawnTime = 20 * 1000;
  21.  
  22. // Poptime represents how long dynamic items (those that are thrown or
  23. // dropped) will last in the world before being deleted.
  24. $Item::PopTime = 10 * 1000;
  25.  
  26.  
  27. //-----------------------------------------------------------------------------
  28. // ItemData base class methods used by all items
  29. //-----------------------------------------------------------------------------
  30.  
  31. //-----------------------------------------------------------------------------
  32.  
  33. function Item::respawn(%this)
  34. {
  35.    // This method is used to respawn static ammo and weapon items
  36.    // and is usually called when the item is picked up.
  37.    // Instant fade...
  38.    %this.startFade(0, 0, true);
  39.    %this.setHidden(true);
  40.  
  41.    // Shedule a reapearance
  42.    %this.schedule($Item::RespawnTime, "setHidden", false);
  43.    %this.schedule($Item::RespawnTime + 100, "startFade", 1000, 0, false);
  44. }   
  45.  
  46. function Item::schedulePop(%this)
  47. {
  48.    // This method deletes the object after a default duration. Dynamic
  49.    // items such as thrown or drop weapons are usually popped to avoid
  50.    // world clutter.
  51.    %this.schedule($Item::PopTime - 1000, "startFade", 1000, 0, true);
  52.    %this.schedule($Item::PopTime, "delete");
  53. }
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57. // Callbacks to hook items into the inventory system
  58.  
  59. function ItemData::onThrow(%this,%user,%amount)
  60. {
  61.    // Remove the object from the inventory
  62.    if (%amount $= "")
  63.       %amount = 1;
  64.    if (%this.maxInventory !$= "")
  65.       if (%amount > %this.maxInventory)
  66.          %amount = %this.maxInventory;
  67.    if (!%amount)
  68.       return 0;
  69.    %user.decInventory(%this,%amount);
  70.  
  71.    // Construct the actual object in the world, and add it to 
  72.    // the mission group so it's cleaned up when the mission is
  73.    // done.  The object is given a random z rotation.
  74.    %obj = new Item() {
  75.       datablock = %this;
  76.       rotation = "0 0 1 " @ (getRandom() * 360);
  77.       count = %amount;
  78.    };
  79.    MissionGroup.add(%obj);
  80.    %obj.schedulePop();
  81.    return %obj;
  82. }
  83.  
  84. function ItemData::onPickup(%this,%obj,%user,%amount)
  85. {
  86.    // Add it to the inventory, this currently ignores the request
  87.    // amount, you get what you get.  If the object doesn't have
  88.    // a count or the datablock doesn't have maxIventory set, the
  89.    // object cannot be picked up.
  90.    %count = %obj.count;
  91.    if (%count $= "")
  92.       if (%this.maxInventory !$= "") {
  93.          if (!(%count = %this.maxInventory))
  94.             return;
  95.       }
  96.       else
  97.          %count = 1;
  98.    %user.incInventory(%this,%count);
  99.  
  100.    // Inform the client what they got.
  101.    if (%user.client)
  102.       messageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickupName);
  103.  
  104.    // If the item is a static respawn item, then go ahead and
  105.    // respawn it, otherwise remove it from the world.
  106.    // Anything not taken up by inventory is lost.
  107.    if (%obj.isStatic())
  108.       %obj.respawn();
  109.    else
  110.       %obj.delete();
  111.    return true;
  112. }
  113.  
  114.  
  115. //-----------------------------------------------------------------------------
  116. // Hook into the mission editor.
  117.  
  118. function ItemData::create(%data)
  119. {
  120.    // The mission editor invokes this method when it wants to create
  121.    // an object of the given datablock type.  For the mission editor
  122.    // we always create "static" re-spawnable rotating objects.
  123.    %obj = new Item() {
  124.       dataBlock = %data;
  125.       static = true;
  126.       rotate = true;
  127.    };
  128.    return %obj;
  129. }
  130.  
  131.